/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pacificlms.MainPanel;

import java.lang.reflect.Method;
import javax.swing.JTabbedPane;
import javax.swing.plaf.TabbedPaneUI;
import pacificlms.Error.ErrorHandle;
import pacificlms.Verifiable.VerifiableObject;

/**
 * CUIP is the parent of all of the Central UI Panels (Action, Search, Information)
 * CUIP provides functions to help with accepting passed objects, and closing
 * @author brian
 */
public abstract class CentralUIPanel extends javax.swing.JPanel {

    /**
     * Creates new form CentralUIPanel
     */
    public CentralUIPanel(VerifiableObject VO) {
        initComponents();
    }

    /**
     * Tab name is the name that will be displayed on the tab in the main screen
     */
    public abstract String TabName();
    /*{
        return "Test panel";
    }*/
    
    protected VerifiableObject CurrentObject = null;
    private CentralUIPanel DataRequestor = null;
    private Method DRCallMethod = null;

    /**
     * Sets the CUIP to pass the information this panel creates to 
     */
    public void SetUIRequestor(CentralUIPanel Requestor, Method Call)
    {
        DataRequestor = Requestor;
        DRCallMethod = Call;
    }
    
    /**
     * Passes the information to the target panel
     */
    public void CallUIRequestor()
    {
        //unfreeze target panel  or  return if not exist
        if (DataRequestor == null)
            return;
        JTabbedPane TP = (JTabbedPane) DataRequestor.getParent();
        int index = TP.indexOfComponent(DataRequestor);
        TP.setEnabledAt(index, true);

        if (DRCallMethod == null)// || CurrentObject == null)//Passing a null is ok
            return;
        
        try
        {
            DRCallMethod.invoke(DataRequestor, CurrentObject);
            DataRequestor.setVisible(true);
        }
        catch(Exception Ex)
        {
            ErrorHandle.Display("Return Error", "Error passing data to waiting tab.\nTry restarting the application.", Ex, true);
        }
    }
    
    /**
     * Closes the panel
     */
    protected void Close()
    {
        MainPanel.GetMainPanel().RemoveCentralUIPanel(this);
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        jButton1.setText("Remove");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(267, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(215, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * Just in case a CUIP is displayed it can be closed by pressing the bottom left button
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        MainPanel.GetMainPanel().RemoveCentralUIPanel(this);
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    // End of variables declaration//GEN-END:variables
}
